﻿/*=========================================================================
 * AUTH: Vincent Breton
 * DESC: Steering Arrive Behavior
 *=========================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D
using Microsoft.Xna.Framework;  //  for Vector2
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace RisingThreat
{
    class SteeringArrive
    {
        public Vector3 target;
        public Steering steer;
        private const float radiusSatisfaction = 1.0f;
        private const float radius = 10.0f;
        private const float timeToArrive = 0.5f;

        public SteeringArrive()
        {
            //target = new Vector3(-30, 0, 15);
        }

        public SteeringArrive(Steering s, Vector3 t)
        {
            steer = s;
            target = t;
        }


        public virtual Steering getSteering(Steering s)
        {
            steer = s;
            arriveHelper();
            return steer;
        }

         public void arriveHelper()
         {
             Vector3 targetVelocity = target - steer.position;
             Vector3 targetSpeed;
             float distance = targetVelocity.Length();

             if (distance < radiusSatisfaction)
             {
                 steer.velocity = new Vector3(0, 0, 0);
                 steer.acceleration = new Vector3(0, 0, 0);
                 return;
             }

             if (distance < radius)
                 targetSpeed = steer.maxVelocity*(distance/radius);
             else
                 targetSpeed = steer.maxVelocity;
             targetVelocity.Normalize();
             targetVelocity*= targetSpeed;
             steer.acceleration = (targetVelocity - steer.velocity);
             steer.acceleration /= timeToArrive;
             if (steer.acceleration.Length() > steer.maxAcceleration.Length())
             {
                 Vector3 temp = steer.acceleration;
                 temp.Normalize();
                 steer.acceleration = temp;
                 steer.acceleration *= steer.maxAcceleration;
             }

         }
    }
         
}
